#####################################################################
# Outcomes sociotropic sample
#####################################################################

rm(list=ls())

library(lmtest)
library(sandwich)
library(stargazer)

# load function that does clustered SEs
vcovCluster <- function(
  model,
  cluster
)
{
  require(sandwich)
  require(lmtest)
  if(nrow(model.matrix(model))!=length(cluster)){
    stop("check your data: cluster variable has different N than model")
  }
  M <- length(unique(cluster))
  N <- length(cluster)           
  K <- model$rank   
  if(M<40){
    warning("Fewer than 40 clusters, variances may be unreliable")
  }
  dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
  uj  <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
  rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
  return(rcse.cov)
}

##############################################
# Read data and prepare basic output
##############################################

# Generate log  
#sink("004_outcomes_socio.txt")

# Read data 
load("002_match_socio.Rdata")

table(d$t_ind)
table(d_match$t_ind)

##########################
# Regressions results
##########################

# Only treatment
mod1a = lm(outcome ~ t_ind, data = d_match)
mod1 <- coeftest(mod1a, vcov = vcovCluster(mod1a, cluster = d_match$bairroibge))

# Treatment + control
mod2a = lm(outcome ~ t_ind + 
                     v22c_presvote + 
                     v41e_thermserra_imp + 
                     v50_ideology_imp, 
                     data = d_match)
mod2 <- coeftest(mod2a, vcov = vcovCluster(mod2a, cluster = d_match$bairroibge))

# Treatment + fixed effects
mod3a = lm(outcome ~ t_ind + 
                     as.factor(bairroibge), 
                     data = d_match)
mod3 <- coeftest(mod3a, vcov = vcovCluster(mod3a, cluster = d_match$bairroibge))

# Treatment + control + fixed effects
mod4a = lm(outcome ~ t_ind + 
                     v22c_presvote + 
                     v41e_thermserra_imp +
                     v50_ideology_imp +
                     as.factor(bairroibge),
                     data = d_match)
mod4 <- coeftest(mod4a, vcov = vcovCluster(mod4a, cluster = d_match$bairroibge))

# TABLE 2 (MANUSCRIPT)
dim(d_match)
stargazer(mod1,mod2,mod3,mod4,
          type = "text",
          title="Regression results sociotropic treatment",
          align=TRUE, 
          omit.stat=c("LL","ser","f","rsq","adj.rsq"), 
          no.space=TRUE,  
          multicolumn = TRUE,
          table.placement = "H",
          #single.row = TRUE,
          #column.separate = c(4,4),
          covariate.labels=c("Negative sociotropic treatment"),
          dep.var.caption  = "Voting for the incumbent",
          dep.var.labels.include = FALSE,
          omit = c("v22c_presvote","v41e_thermserra_imp","v50_ideology_imp","bairroibge","Constant"),
          add.lines = list(c("Controls","No","Yes","No","Yes"),
                           c("Neighborhood Fixed effects","No","No","Yes","Yes"),
                           c("Observations","316","316","316","316")))
          
#sink()
